import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:FiduMart/localization/app_export.dart';
import 'package:easy_refresh/easy_refresh.dart';

import '../../app_routes.dart';
import '../../constants/constants.dart';
import '../../core/network/market_news_list_entity.dart';
import '../../service/setting.dart';
import '../page1/page1_controller.dart';

class Page2 extends StatefulWidget {
  @override
  State<Page2> createState() => _Page2State();
}

class _Page2State extends State<Page2> {

  final controller = Get.put(Page1Controller());
  String _selectedValue = "locale".tr;

  final List<String> _dropdownValues = langmap.keys.toList();

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: EasyRefresh(
          onRefresh: () async {
            controller.getNewsList();
          },
          child:Container(
            padding: EdgeInsets.only(left: 10,right: 10),
            child: Column(

              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('home_Information'.tr,style: TextStyle(fontSize: 16,color: Colors.black),),
                    TextButton(onPressed: (){

                    }, child: DropdownButton<String>(
                      value: _selectedValue,
                      icon: Image(image: AssetImage("assets/images/changeLangchooseupBtnImg.png"),width: 20,),
                      underline: Container(),
                      style: TextStyle(fontSize: 14),
                      onChanged: (String? newValue) {
                        setState(() {
                          _selectedValue = newValue!;
                          Setting.updateLang(_selectedValue);
                        });
                      },
                      items: _dropdownValues.map((String value) {
                        return DropdownMenuItem<String>(
                          value: value,
                          child: Text(langmap[value]!,style: TextStyle(color: ColorConstant.colorFF707280),),
                        );
                      }).toList(),
                    ),)

                    // CustomImageView(
                    //   imagePath: "assets/images/market_language.png",
                    //   width: 25,
                    //   height: 25,
                    // ),
                  ],
                ),
                FutureBuilder(
                  future: controller.getNewsList(),
                  builder: (BuildContext context, AsyncSnapshot snapshot) {
                    if (snapshot.connectionState == ConnectionState.done) {
                      if (snapshot.hasError) {
                        return Center(
                            child: Text("${snapshot.error}",
                                style: TextStyle(fontSize: 20)));
                      } else {
                        return Obx(() => controller.newsList.length == 0
                            ? nodataView()
                            : informationListView());
                      }
                    } else {
                      return Center(child: CircularProgressIndicator());
                    }
                  },
                ),
                // informationListView(),
              ],
            ),
          )

    ));
  }

  Widget informationListView(){
    return Expanded(child:
      ListView.separated(
      itemCount: controller.newsList.length,
      padding: EdgeInsets.zero,
      itemBuilder: (ctx, index) {
        return whiteInformationListItem(controller.newsList[index]);
      },
      separatorBuilder: (BuildContext context, int index) {
      return SizedBox(width: 20);
    }, ));
  }

  Widget nodataView() {
    return Center(
        child: Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                height: 50,
              ),
              CustomImageView(
                width: 70,
                height: 70,
                imagePath: "assets/images/noDate.png",
              ),
              Text("no_relevant_data_yet".tr,style: TextStyle(color: ColorConstant.text_grey),),
            ],
          ),
        ));
  }

  Widget whiteInformationListItem(MarketNewsListEntity newsList){
    return GestureDetector(
      onTap: (){
        Get.toNamed(AppRoutes.webviewScreen, parameters: {
          "html": newsList.contentList![0].lang == "lang".tr ? newsList.contentList![0].content! : newsList.contentList![1].content!,
          "title": "${newsList.contentList![0].lang == "lang".tr ? newsList.contentList![0].title : newsList.contentList![1].title}",
          "time":"${newsList.createTime}",
        },);
      },
      child: Container(
          margin: EdgeInsets.only(bottom: 15),
          padding: EdgeInsets.all(10),
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [ColorConstant.colorFFF2F2F7,ColorConstant.colorFFF2F2F7],
              begin: FractionalOffset.centerLeft,
              end: FractionalOffset.centerRight,
            ),
            borderRadius: BorderRadius.circular(10),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(child:
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('${newsList.contentList![0].lang == "lang".tr ? newsList.contentList![0].title : newsList.contentList![1].title}',
                    style: TextStyle(fontSize: 15,color: Colors.black),maxLines: 2,textAlign: TextAlign.left,
                    softWrap: true,overflow: TextOverflow.ellipsis,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          CustomImageView(
                            width: 15,
                            height: 15,
                            imagePath: "assets/images/fidumart_logo.png",
                          ),
                          SizedBox(width: 5,),
                          Text('FiduMart',style: TextStyle(fontSize: 13,color: Colors.grey),),
                        ],
                      ),
                      Text("${newsList.createTime}",style: TextStyle(fontSize: 13,color: Colors.grey),)
                    ],
                  )
                ],
              ),
              ),
              SizedBox(width: 20,),
              CustomImageView(
                height: 60,
                width: 60,
                url: newsList.contentList![0].lang == "lang".tr ? newsList.contentList![0].coverUrl : newsList.contentList![1].coverUrl,
              ),
            ],
          )
      ),
    );
  }

  Widget blackInformationListItem(MarketNewsListEntity newsList){
    return GestureDetector(
      onTap: (){
        Get.toNamed(AppRoutes.webviewScreen, parameters: {
          "html": newsList.contentList![0].lang == "lang".tr ? newsList.contentList![0].content! : newsList.contentList![1].content!,
          "title": "${newsList.contentList![0].lang == "lang".tr ? newsList.contentList![0].title : newsList.contentList![1].title}",
          "time":"${newsList.createTime}"
        },);
      },
      child: Container(
          margin: EdgeInsets.only(bottom: 15),
          padding: EdgeInsets.all(10),
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [ColorConstant.colorFF1C222E,ColorConstant.colorFF1C222E],
              begin: FractionalOffset.centerLeft,
              end: FractionalOffset.centerRight,
            ),
            borderRadius: BorderRadius.circular(10),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CustomImageView(

                url: newsList.contentList![0].lang == "lang".tr ? newsList.contentList![0].coverUrl : newsList.contentList![1].coverUrl,
              ),
              SizedBox(height: 5,),
              Text('${newsList.contentList![0].lang == "lang".tr ? newsList.contentList![0].title : newsList.contentList![1].title}',
                style: TextStyle(fontSize: 15,color: Colors.white),maxLines: 1,textAlign: TextAlign.left,
                softWrap: true,overflow: TextOverflow.ellipsis,
              ),
              Row(
                children: [
                  CustomImageView(
                    width: 15,
                    height: 15,
                    imagePath: "assets/images/fidumart_logo.png",
                  ),
                  SizedBox(width: 5,),
                  Text('FiduMart',style: TextStyle(fontSize: 13,color: Colors.grey),),
                ],
              )
            ],
          ),
      ),
    );
  }
}